<?php



class MysqlDriver extends DBDriver
{
	private $resultvar;
	
	public function __construct()
	{
		$this->query = array(
			'type' => 'none',
			'select' => null,
			'from' => null,
			'where' => null,
			'order' => null,
			'limit' => null
		);
	}
	
	public function connect( $host, $user, $password, $database )
	{
		$this->resource = mysql_connect( $host, $user, $password, true );
		if ( $this->resource == false )
		{
			trigger_error("Error connection to database");
		}
		
		mysql_select_db( $database, $this->resource );
	}
	
	
	public function __call( $method, $args )
	{
		parent::_call( $method, $args, __CLASS__ );
	}
	
	
	public function select( $fields, $table )
	{
		$this->query['type'] = 'select';
		$this->query['select'] = $fields;
		$this->query['from'] = $table;
		
		return $this;
	}
	
	
	
	public function where( $usl )
	{
		$this->query['where'] = $usl;
		
		return $this;
	}
	
	
	public function order( $field, $asc = "asc" )
	{
		$this->query['order'] = array($field, $asc);
		
		return $this;
	}
	
	
	public function limit( $start, $count )
	{
		$this->query['limit'] = array($start, $count);
		
		return $this;
	}
	
	
	public function query( $query )
	{
		$this->query = $query;
	}
	
	
	private function string_query()
	{
		if ( assert( is_string( $this->query ) ) )
		{
			return mysql_query( $this->query, $this->resource );
		}
		
		return null;
	}
	
	
	public function insert( $table )
	{
		$q = array();
		$q['type'] = 'insert';
		$q['table'] = $table;
		$this->query = $q;
		
		unset($q);
		
		return $this;
	}
	
	
	public function remove( $table )
	{
		$q = array();
		$q['type'] = 'remove';
		$q['table'] = $table;
		
		$this->query = $q;
		unset($q);
		return $this;
	}
	
	
	public function truncate( $table )
	{
		$q = array();
		$q['type'] = 'truncate';
		$q['table'] = $table;
		
		$this->query = $q;
		unset($q);
		return $this;
	}
	
	
	public function values( $fields, $values )
	{
		if ( is_array($fields) && is_array($values) )
		{
			if ( count( $fields ) == count($values) )
			{
				$f = "";
				$v = "";
				$cnt = count($fields);
				
				for( $i = 0; $i < $cnt; $i++ )
				{
					if ( $i > 0 )
					{
						$f .= ", ";
						$v .= ", ";
					}
					
					$f .= "`" . $fields[$i] . "`";
					$v .= "" . ( $values[$i] == null ) ?
								"NULL" : "'" . $values[$i] . "'";
				}
				
				$this->query['fields'] = $f;
				$this->query['values'] = $v;
			}
			else
			{
				trigger_error( "Fields and Values arrays must have same length!" );
			}
		}
		else
		{
			$this->query['fields'] = $fields;
			$this->query['values'] = $values;
		}
		return $this;
	}
	
	
	public function result()
	{
		$q = $this->query;
		
		if ( is_string( $q ) )
		{
			$res = $this->string_query();
		}
		
		if ( assert( is_array( $q ) ) )
		{
			if ( $q['type'] == 'select' )
			{
				$qq = 'SELECT ' . mysql_real_escape_string($q['select'], $this->resource)
					. ' FROM `' . mysql_real_escape_string($q['from'], $this->resource) . "`";
				
				if ( $q['where'] )
				{
					$qq .= ' WHERE ' . $q['where'];
				}
				
				if ( $q['order'] )
				{
					$qq .= ' ORDER BY `'
						. mysql_real_escape_string($q['order'][0], $this->resource)
						. '` ' . (strncasecmp($q['order'][1], "asc", strlen("asc")) ? "ASC" : "DESC");
				}
				
				if ( $q['limit'] )
				{
					$qq .= ' LIMIT ' . $q['limit'][0] . "," . $q['limit'][1] . "";
				}
				
				$qq .= ";";
				
				assert( $res = mysql_query( $qq, $this->resource ) );
			}
			elseif ( $q['type'] == 'insert' )
			{
				$qq = 'INSERT INTO `' . mysql_real_escape_string($q['table'], $this->resource)
					. "` (". $q['fields'] .") VALUES (". $q['values'] .');';
				
				assert( $res = mysql_query( $qq, $this->resource ) );
			}
			elseif ( $q['type'] == 'remove' )
			{
				$qq = 'DELETE FROM `' . $q['table'] . '` WHERE ' . $q['where'] . ";";
				assert( $res = mysql_query( $qq, $this->resource ) );
			}
			elseif ( $q['type'] == 'truncate' )
			{
				$qq = 'TRUNCATE TABLE `' . $q['table'] . '`;';
				assert( $res = mysql_query( $qq, $this->resource ) );
			}
			else
			{
				$res = null;
			}
		}
		else
		{
			$res = null;
		}
		
		$this->resultvar = $res;
		
		assert( $res );
		
		unset($res);
		
		return $this;
	}
	
	
	public function fetch_bool()
	{
		return $this->resultvar ?: false;
	}
	
	
	public function fetch_array()
	{
		$res = $this->resultvar;
		
		if ( $res != null && mysql_num_rows( $res ) )
		{
			return mysql_fetch_array( $res );
		}
		
		return null;
	}
	 
	
	public function fetch_assoc()
	{
		$res = $this->resultvar;
		
		if ( assert($res) )
		{
			return mysql_fetch_assoc( $res );
		}
		
		return null;
	}
}



